package com.xstablet.view.popuputil;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.db.shared_prefrences.interfaces.UserCacheInterface;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.json_deal.lib_mgson.MGson;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.http.call_back.HttpNormalCallBack;
import com.xstablet.R;
import com.xstablet.bean.MainInfoBean_1;
import com.xstablet.bean.TipsReadStateBean;
import com.zyyoona7.popup.EasyPopup;

import tablet.module_user.bean.BaseLoginBean;

public class TabletMoneyTypePopup {

    private Activity activity;

    public TabletMoneyTypePopup(Activity activity) {

        this.activity = activity;

        initPopuPopu();
    }

    private EasyPopup popu;

    private void initPopuPopu() {

        popu = EasyPopup.create()
                .setContext(activity)
                //是否允许点击PopupWindow之外的地方消失
                .setFocusAndOutsideEnable(false)
                .setContentView(R.layout.dialog_tablettype)
                .setWidth(WindowManager.LayoutParams.FILL_PARENT)
                .setHeight(WindowManager.LayoutParams.FILL_PARENT)
                .setOnViewListener(new EasyPopup.OnViewListener() {
                    @Override
                    public void initViews(View view, EasyPopup basePopup) {

                        initView(view);

                    }
                })
                .setFocusAndOutsideEnable(true)
                .apply();

    }

    TextView tvShowType;
    LinearLayout linFirst, linRecharge;
    //第一次进入显示购买的套餐类型和赠送的天数
    TextView tvType, tvSendNum;
    //充值成功，显示充值的次数和剩余的次数
    TextView tvrechargeNum, tvRestNum;
    TextView tvSure;

    private void initView(View view) {

        tvShowType = view.findViewById(R.id.tv_showtype);

        linFirst = view.findViewById(R.id.lin_first);
        linRecharge = view.findViewById(R.id.lin_recharge);

        tvType = view.findViewById(R.id.tv_type);
        tvSendNum = view.findViewById(R.id.tv_num);

        tvrechargeNum = view.findViewById(R.id.tv_rechargeNum);
        tvRestNum = view.findViewById(R.id.tv_restnum);

        tvSure = view.findViewById(R.id.tv_sure);

        tvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (firstOrRecharge > -1) {

                    //只有充值成功才会去取消阅读状态
                    if (1 == firstOrRecharge) {

                        //发送已阅读状态到服务器
                        sendReadToSerVice();
                    }
                }else {

                    //将数据设置为1，方便外部判断
                    UserCacheInterface userCacheInterface = SharedPreferenceManager.getInstance(activity).getUserCache();
                    userCacheInterface.setIsFirtShowMainFragment(1);

                }

                dismiss();
            }
        });

    }

    private void sendReadToSerVice() {

        HttpUtils.with(activity)
                .addParam("requestType", "WHETHER_TO_READ")
                .addParam("token", SharedPreferenceManager.getInstance(activity).getUserCache().getUserToken())
                .addParam("id", rechargeId)
                .addParam("whether_to_read", 1)
                .execute(new HttpNormalCallBack<TipsReadStateBean>() {
                    @Override
                    public void onSuccess(TipsReadStateBean result) {

                        if (CodeUtil.CODE_200 == result.getCode()) {

                            dismiss();
                        }
                    }

                    @Override
                    public void onError(String e) {

                    }
                });


    }

    //判断是购买还是充值的类型
    int firstOrRecharge = -1;

    //获取本次充值的Id，记录阅读状态
    int rechargeId = -1;

    /**
     * @param type     用来区分第一次进入和充值  0-----为第一次进入   1----为充值成功
     * @param dataJson 用户购买以及充值的具体数据
     */
    public void updateTypeData(int type, String dataJson, int restNum) {


        if (StringUtil.isNotEmpty(dataJson, true)) {

            firstOrRecharge = type;

            if (0 == type) {

                BaseLoginBean.ObjBean.PackageContentBean contentBean = MGson.newGson()
                        .fromJson(dataJson, BaseLoginBean.ObjBean.PackageContentBean.class);

                tvShowType.setText("Hi~欢迎来到学思兔");
                linFirst.setVisibility(View.VISIBLE);
                linRecharge.setVisibility(View.GONE);

                tvType.setText(contentBean.getSet_meal_price() + "套餐");
                if (contentBean.getGift_times() == 0) {
                    tvSendNum.setText(contentBean.getNumber_classes() + "次");
                } else if (contentBean.getGift_times() > 0) {
                    tvSendNum.setText(contentBean.getNumber_classes() + "(+赠送" + contentBean.getGift_times() + ")" + "次");
                }

            } else if (1 == type) {

                tvShowType.setText("Hi~恭喜您充值成功");
                linFirst.setVisibility(View.GONE);
                linRecharge.setVisibility(View.VISIBLE);

                MainInfoBean_1.ObjBean .RechargeBean rechargeBean = MGson.newGson()
                        .fromJson(dataJson, MainInfoBean_1.ObjBean.RechargeBean.class);

                rechargeId = rechargeBean.getId();

                if (rechargeBean.getAdding_data() > 0) {
                    tvrechargeNum.setText(rechargeBean.getAdding_data() + "(+赠送" + rechargeBean.getNumber_gifts() + ")" + "次");
                }

                if (rechargeBean.getNumber_gifts() > 0) {
                    tvRestNum.setText(restNum + "次");
                }
            }
        }

    }

    public void dismiss() {
        popu.dismiss();
    }


    public void showAnswerPopuPopu(View view) {
        popu.showAtLocation(view, Gravity.CENTER, 0, 0);

    }

}
